package com.Webserver.core;

import com.Webserver.controller.ArticleController;
import com.Webserver.controller.UserController;
import com.Webserver.http.HttpRequest;
import com.Webserver.http.HttpResponse;

import java.io.File;

/**
 * 用于处理请求.
 */
public class DispatcherServlet {
    public void server(HttpRequest request, HttpResponse response) {
        String path = request.getRequestURI();
        if ("/myweb/regUser".equals(path)) {
            UserController controller = new UserController();
            controller.reg(request, response);
        } else if ("/myweb/loginUser".equals(path)) {
            UserController controller = new UserController();
            controller.login(request, response);
        } else if("/myweb/writeArticle".equals(path)) {
            ArticleController controller = new ArticleController();
            controller.writeArticle(request,response);
        } else if("/myweb/showAllUser".equals(path)) {
            UserController controller = new UserController();
            controller.showAllUser(request, response);
        } else if("/myweb/showAllArticle".equals(path)) {
           ArticleController controller = new ArticleController();
           controller.showAllArticle(request,response);
        } else{
            File file = new File("./webapps" + path);
            if (file.exists() && file.isFile()) {
                response.setEntity(file);
            } else {
                response.setStatusCode(404);
                response.setStatusReason("NotFound");
                file = new File("./webapps/root/404.html");
                response.setEntity(file);
            }
        }
        response.putHeader("Server", "WebServer");
    }
}
